<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/25
 * Time: 15:15
 */

/*
易优网-短信发送示例
$account='00001';
$password='123456';
$mobile='13986175579';
$content='您的验证码是【'.mt_rand(100000,999999).'】。【优易网】';  //发送内容
$time='';     //发送时间，为空时是即时发送
$extno='1069032239089';
$res = sendSMS($account,$password,$mobile,$content,$extno);
echo $res;
*/
function sendSMS($mobile, $orderNo)
{

    $content = "【手淘科技】尊敬的用户您好，您的公众号搭建订单已付款成功！请关注QQ或QQ邮箱消息，客服将在2个工作日内与您联系，指导您搭建安装，谢谢！";
    $http = 'http://www.smswang.net:7803/sms';
    $data = array
    (
        'action'=>"send",
        'account'=>"001207",					//账户密码
        'password'=>"123456",			//账户密码
        'mobile'=>$mobile,				//目标号码
        'content'=>$content,			//
        'extno'=>"106903223908910",     //接入码
    );
    $re= postSMS($http,$data);			//POST方式提交
//    var_dump($re);exit;
    if(stristr($re,'OK'))
    {
        return "0"; //发送成功
    }
    else
    {
        return "发送失败! XML信息".$re;
    }
}


//订单十分钟未付款通知
function sendSMS_order($mobile,$id,$name)
{

    $content = "【起邦科技】尊敬的$name ，您的公众号申请订单已生成，排单顺序以支付完成时间为准，请及时支付，以免名称被他人注册！该订单支付链接：http://weixin.gskprfg.com/home/index/smallprogram_apply_pay/uuid/$id";
    $http = 'http://www.smswang.net:7803/sms';
    $data = array
    (
        'action'=>"send",
        'account'=>"001207",					//账户密码
        'password'=>"123456",			//账户密码
        'mobile'=>$mobile,				//目标号码
        'content'=>$content,			//
        'extno'=>"106903223908910",     //接入码
    );
//    dump($data);
    $re= postSMS($http,$data);			//POST方式提交
    if(stristr($re,'OK'))
    {
        return "0"; //发送成功
    }
    else
    {
        return "发送失败! XML信息".$re;
    }
}

function postSMS($url,$data='')
{
    $row = parse_url($url);
    $row['port']='7803';
    $host = $row['host'];
    $port = $row['port'] ? $row['port']:80;
    $file = $row['path'];
    $post='';
    while (list($k,$v) = each($data))
    {
        $post .= rawurlencode($k)."=".rawurlencode($v)."&";	//תURL��׼��
    }
    $post = substr( $post , 0 , -1 );
    $len = strlen($post);
    $fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
    if (!$fp) {
        return "$errstr ($errno)\n";
    } else {
        $receive = '';
        $out = "POST $file HTTP/1.1\r\n";
        $out .= "Host: $host\r\n";
        $out .= "Content-type: application/x-www-form-urlencoded\r\n";
        $out .= "Connection: Close\r\n";
        $out .= "Content-Length: $len\r\n\r\n";
        $out .= $post;
        fwrite($fp, $out);
        while (!feof($fp)) {
            $receive .= fgets($fp, 128);
        }
        fclose($fp);
        $receive = explode("\r\n\r\n",$receive);
        unset($receive[0]);
        return implode("",$receive);
    }
}

function http_post($url, $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $res = curl_exec($ch);
    curl_close($ch);
    return $res;
}

/**
 * php发送http post请求
 * @param $url
 * @param $jsonStr
 * @return array
 */
function http_post_json($url, $jsonStr)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr)
        )
    );
    $response = curl_exec($ch);
    return $response;
/*
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    var_dump($response) ;
    var_dump(json_decode($response));
    return array($httpCode, $response);*/
}